<?php

namespace common\models;

use Yii;

/**
 * This is the model class for table "bus_health".
 *
 * @property integer $health_id
 * @property string $health_name
 * @property integer $pid
 */
class BusHealth extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'bus_health';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['health_name'], 'required'],
            [['pid'], 'integer'],
            [['health_name'], 'string', 'max' => 50]
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'health_id' => 'Health ID',
            'health_name' => '体质名称',
            'pid' => '上级体质id',
        ];
    }

    /**
     * 获取所有的体质列表（适用于checkboxList的树结构）
     * @return array
     */
    public function getAll()
    {
        $list = self::find()->where(['pid' => 0])->asArray()->all();

        foreach ($list as $key => $val) {
            $list[$key]['_item'] = self::find()->where(['pid' => $val['health_id']])->asArray()->all();
        }

        return $list;
    }

    /**
     * 检查会员是否有某个体质
     * @param array $healthList 会员的体质信息数组
     * @param int $health_id 体质id
     * @return bool
     */
    public function checkHealth($healthList, $health_id)
    {
        foreach ($healthList as $val) {
            if ($val['attr_id'] == $health_id)
                return true;
        }
        return false;
    }
}
